/*
 * @Author: yehongbo 1394168736@qq.com
 * @Date: 2024-06-26 16:10:41
 * @LastEditors: yehongbo 1394168736@qq.com
 * @LastEditTime: 2024-07-29 14:12:56
 * @FilePath: \myWorkspace\gaxic-protection-settingbehavior\src\utils\request.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios"
// import { useRoute } from "vue-router"
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from "axios"
import { ElMessage } from "element-plus"
import { toLogin } from '@/utils/util'

// type Result = {
//   code: number
//   message: string
//   result: any
// }
class Request {
  // axios 实例
  instance: AxiosInstance;
  // 基础配置，url和超时时间
  baseConfig: AxiosRequestConfig = {
    // baseURL: ["production", "test"].includes(process.env.NODE_ENV as string)
    //   ? process.env.VUE_APP_BASE_URL
    //   : "",
    // baseURL: "http://192.168.230.101:8088",
    // baseURL: "http://192.168.40.20:8088",
    baseURL: "/bhdzxgxwsb",
    timeout: 1000000,
    headers: {
      'access-token': localStorage.getItem('accessToken')
    }
  };

  constructor(config: AxiosRequestConfig) {
    // 使用axios.create创建axios实例
    this.instance = axios.create(Object.assign(this.baseConfig, config));
    this.instance.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        return config;
      },
      (err: any) => {
        return Promise.reject(err);
      }
    );

    this.instance.interceptors.response.use(
      (res: AxiosResponse) => {
        // 直接返回res，当然你也可以只返回res.data
        const newToken = res.headers["New-Access-Token"]
        if (newToken) {
          localStorage.setItem('accessToken', newToken)
        }
        // console.log('response res', res)
        if (res.data && (res.data.code == 99 || res.data.errorCode == 401)) { // 未登录
          ElMessage.error(res.data.errorMsg || res.data.msg || res.data.message)
          toLogin();
          return Promise.reject(res.data);
        }

        return res;
      },
      (err: any) => {
        console.log("---------请求错误信息-------", err);

        if (err.response?.status == 401) {
          let msg = err.response?.data?.data || '登录超时';
          ElMessage.error(msg);
          err.message = msg;
          toLogin();
        } else {
          ElMessage.error("请检查网络或联系管理员！")
        }
        // 这里是AxiosError类型，所以一般我们只reject我们需要的响应即可
        return Promise.reject(err.response);
      }
    );
  }

  // 定义请求方法
  public request(config: AxiosRequestConfig) {
    return this.instance.request(config);
  }

  public get(url: string, config?: AxiosRequestConfig) {
    return this.instance.get(url, config);
  }
  public post(url: string, data?: any, config?: AxiosRequestConfig) {
    return this.instance.post(url, data, config);
  }
  public put(url: string, data?: any, config?: AxiosRequestConfig) {
    return this.instance.put(url, data, config);
  }

  public delete(url: string, config?: AxiosRequestConfig) {
    return this.instance.delete(url, config);
  }
}
export default Request
